package com.mitu.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * NIO使用transferFrom和FileChannel拷贝文件
 */
public class NIOFileChannel4 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\hello.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\hello2.jpg");

        FileChannel srcFileChannel = fileInputStream.getChannel();
        FileChannel dstFileChannel = fileOutputStream.getChannel();

        //使用transferFrom完成拷贝
        dstFileChannel.transferFrom(srcFileChannel, 0, srcFileChannel.size());
        //关闭通道和关闭流
        srcFileChannel.close();
        dstFileChannel.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}
